package com.taru.service.impl;

import com.taru.dao.MessageMapper;
import com.taru.model.Message;
import com.taru.service.MessageService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    private static Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);

    @Autowired
    MessageMapper messageMapper;

    @Override
    public int insertMessage(Message message) {
        return messageMapper.insertMessage(message);
    }

    @Override
    public int updateMessageById(Message  message) {
        return messageMapper.updateMessageById(message);
    }

    @Override
    public int deleteMessageById(String userId) {
        return messageMapper.deleteMessageById(userId);
    }

    @Override
    public List<Message>  queryMessageById(@Param("fruitsBatch") String fruitsBatch, @Param("userId")  String userId) {
        return messageMapper.queryMessageById(fruitsBatch,userId);
    }

    @Override
    public List<Message> queryMessageByBatch(String userId, String ietm) {
       List<Message> message= messageMapper.queryMessageByBatch(userId);
        List<Message> messageList = new ArrayList<Message>();
        if(ietm.equals("messageWeather")){
            for (Message list : message) {
                if(list.getMessageWeather() != null || list.getMessageWeather().length() !=0){
                    messageList.add(list);
                }
            }
        }else if(ietm.equals("messageWatering")){
            for (Message list : message) {
                if(list.getMessageWatering() !=  null || list.getMessageWatering().length() != 0){
                    messageList.add(list);
                }
            }
        }else if(ietm.equals("messagePesticides")){
            for (Message list : message) {
                if(list.getMessagePesticides() != null || list.getMessagePesticides().length() != 0){
                    messageList.add(list);
                }
            }
        }else if(ietm.equals("messageFertilization")){
            for (Message list : message) {
                if(list.getMessageFertilization() != null || list.getMessageFertilization().length() != 0){
                    messageList.add(list);
                }
            }
        }else if(ietm.equals("messageBagging")){
            for (Message list : message) {
                if(list.getMessageBagging() != null || list.getMessageBagging().length() != 0){
                    messageList.add(list);
                }
            }
        }else if(ietm.equals("messageWeeding")){
            for (Message list : message) {
                if(list.getMessageWeeding() != null || list.getMessageWeeding().length() != 0){
                    messageList.add(list);
                }
            }
        }
        return  messageList;
    }





}
